using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using HarmonyLib;

namespace TheWorldOfKongfuEasyMod;

[HarmonyPatch(typeof(LevelUpController))]
[HarmonyPatch("Roll")]
public static class Patch_LevelUpController_Roll
{
    static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
    {
        if (Main.Setting._009multipleTalentValue.Value==1)
            return instructions;
        var codeMatcher = new CodeMatcher(instructions /*, ILGenerator generator*/);

        codeMatcher.MatchForward(false, new CodeMatch(OpCodes.Ldc_I4_1)
        );
        if (!codeMatcher.IsValid)
        {
            throw new InvalidOperationException("Can't find ldc.i4.1");
        }

        codeMatcher.RemoveInstruction()
            .InsertAndAdvance(
                new CodeInstruction(OpCodes.Ldc_I4_S, Main.Setting._009multipleTalentValue.Value)
            );

        return codeMatcher.Instructions();
    }
}